package com.java2007.zhangyu.restaurant.service.impl;

import com.java2007.zhangyu.restaurant.dao.OrderDao;
import com.java2007.zhangyu.restaurant.dao.OrderDetailDao;
import com.java2007.zhangyu.restaurant.dao.impl.OrderDaoImpl;
import com.java2007.zhangyu.restaurant.dao.impl.OrderDetailDaoImpl;
import com.java2007.zhangyu.restaurant.entity.CartItem;
import com.java2007.zhangyu.restaurant.entity.Result;
import com.java2007.zhangyu.restaurant.pojo.Admin;
import com.java2007.zhangyu.restaurant.pojo.Order;
import com.java2007.zhangyu.restaurant.pojo.OrderDetail;
import com.java2007.zhangyu.restaurant.service.OrderService;
import com.java2007.zhangyu.restaurant.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zy
 * @Version
 * @date 2021/01/29
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();

    /**
     *
     * @param cart
     * @param cartTotalPrice
     * @param tId
     * @return
     */
    @Override
    public Result createOrder(List<CartItem> cart, Long cartTotalPrice, Integer tId) {

        try {

            JdbcUtils.begin();

            //订单数据
            Order order = new Order();
            order.setoId(System.currentTimeMillis());
            order.settId(tId);
            //订单总价
            order.setTotalPrice(cartTotalPrice);
            //支付状态
            order.setoPayState(0);
            order.setoCreateTime(new Date());
            order.setoUpdateTime(new Date());
            order.setoCreateUser(1);  //自己在完成前台登录后，获取创建订单的人

            //保存订单
            orderDao.save(order);
            Thread.sleep(20);

            //集合中是订单详情的数据
            List<OrderDetail> orderDetailList = new ArrayList<>();
            //获取List<CartItem> -->  每个CartItem就是OrderDetail
            //保存订单详情
            for (CartItem cartItem : cart) {
                OrderDetail orderDetail = cartItem;
                //订单详情id
                orderDetail.setOdId(System.currentTimeMillis());
                //订单详情 订单id
                orderDetail.setoId(order.getoId());
                Thread.sleep(20);
                orderDetailDao.save(orderDetail);
                //订单详情数据放入集合中
                orderDetailList.add(orderDetail);
            }

            JdbcUtils.commitAndClose();
            //集合中是传给页面的数据
            Map<String, Object> orderInfo = new HashMap<>();
            orderInfo.put("oId", order.getoId());
            orderInfo.put("total_price", order.getTotalPrice());
            orderInfo.put("orderDetailList", orderDetailList);

            return Result.ok("订单创建成功", orderInfo);
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollbackAndClose();
            return Result.fail("订单创建失败");
        }
    }

    /**
     * 更新订单状态
     * @param orderId
     * @param status
     * @return
     * @throws SQLException
     */
    @Override
    public int updateOrderStatus(Long orderId, int status) throws SQLException {
        return orderDao.updateOrderStatus(orderId, status);
    }

    /**
     * 查询所有订单
     * @return
     * @throws SQLException
     */
    @Override
    public List<Order> queryAllOrder() throws SQLException {
        return orderDao.queryAllOrder();
    }

}
